<?php
namespace Admin\Models\group;

use Admin\Models\BaseModel;

class XhsysGroup extends BaseModel
{

    /**
     *
     * @var integer
     * @Primary
     * @Column(type="integer", length=11, nullable=false)
     */
    public $iGroupId;

    /**
     *
     * @var string
     * @Column(type="string", length=50, nullable=true)
     */
    public $sName;

    /**
     *
     * @var integer
     * @Column(type="integer", length=4, nullable=true)
     */
    public $iType;

    /**
     *
     * @var integer
     * @Column(type="integer", length=11, nullable=true)
     */
    public $iDepId;

    /**
     *
     * @var integer
     * @Column(type="integer", length=4, nullable=true)
     */
    public $bDelete;

    /**
     *
     * @var string
     * @Column(type="string", nullable=true)
     */
    public $tCreateDate;

    /**
     *
     * @var integer
     * @Column(type="integer", length=20, nullable=true)
     */
    public $iCreateUserId;

    /**
     *
     * @var string
     * @Column(type="string", nullable=true)
     */
    public $tLastModifyDate;

    /**
     *
     * @var integer
     * @Column(type="integer", length=20, nullable=true)
     */
    public $iLastModifyUserId;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        parent::initialize();
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'XHSys_Group';
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhsysGroup[]|XhsysGroup
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhsysGroup
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

    public function beforeCreate()
    {
        $check = self::count([
            'conditions' => 'sName = :name:',
            'bind' => ['name' => $this->sName]
        ]);
        if ($check) {
            throw new \Exception('该名称已存在');
        }
        return parent::beforeCreate();
    }
}
